﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Utils
{
   public  class ThreadUtil
    {
       public delegate int MyDelegate(Object voteRe);
       public delegate int DoGreeting(Object voteRe);

      // 线程操作投票
       //  public static int ThreadHandleVote(VoteRecordModel voteRe)
       //{
       //    ThreadMessage("Main Thread");

            
       //    //建立委托
       //    MyDelegate myDelegate = new MyDelegate(Voting);

       //    //同步调用委托
       //    var result = myDelegate.BeginInvoke(voteRe, null, null);
       //    int data = myDelegate.EndInvoke(result);

       //    return data;

       //}

       /// <summary>
       /// 线程调用含有VoteRecordModel参数的方法
       /// </summary>
       /// <param name="voteRe">参数</param>
       /// <param name="hehe">传入自定义的方法</param>
       /// <returns></returns>
       public static int ThreadHandleVote(Object voteRe, DoGreeting hehe)
         {
             ThreadMessage("Main Thread");


             //建立委托
             MyDelegate myDelegate = new MyDelegate(hehe);

             //同步调用委托
             var result = myDelegate.BeginInvoke(voteRe, null, null);
             int data = myDelegate.EndInvoke(result);

             return data;

         }

       static void ThreadMessage(string data)
       {
           string message = string.Format("{0}\n  ThreadId is:{1}",
                  data, Thread.CurrentThread.ManagedThreadId);
           Console.WriteLine(message);
       }

    }
}


